package com.company.nuwa.common.utils;

import com.company.nuwa.common.enumration.GlobalBizCode;
import com.company.nuwa.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
 * <p>描述</p>
 *
 * @author Lionel Lee
 * @version 1.0: UserUtils.java
 * @date 2021/5/5 11:52 上午
 */
@Slf4j
public class UserClaimUtils {

    private static final ThreadLocal<String> TOKEN_THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 移除
     */
    public static void removeToken() {
        TOKEN_THREAD_LOCAL.remove();
    }

    public static String getUserClaim() {
        return TOKEN_THREAD_LOCAL.get();
    }

    public static void setUserClaim(String userClaim) {
        TOKEN_THREAD_LOCAL.set(userClaim);
    }

    public static <T> T getUserInfo(Class<T> clazz) {
        try {
            return JsonUtils.jsonToObject(TOKEN_THREAD_LOCAL.get(), clazz);
        } catch (final Exception e) {
            log.error("获取当前登陆用户信息异常:{}", ExceptionUtils.getStackTrace(e));
            throw new BizException(GlobalBizCode.SERVICE_BUSY);
        }
    }
}
